/**
 * 上海中赢金融信息服务有限公司
 * Copyright (c) 2016-2026 HMC,Inc.All Rights Reserved.
 */

package com.sys.midware.assist.serialize;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.apache.log4j.Logger;

/**
 * <b>ClassName:</b> SerializeUtil.java <br/>
 * <b>Description:</b> 序列号工具类 <br/>
 * <b>Date:</b>     2015年10月22日 下午5:58:29<br/> 
 * @author   mobing
 * @version  	 
 */
public class SerializeUtil {
    private static Logger logger = Logger.getLogger(SerializeUtil.class);

    /**
     * 序列化
     * 
     * @param object
     * @return
     * @throws Exception
     */
    public static byte[] serialize(Object object)  {
        if(object == null) return null;
        ObjectOutputStream oos = null;
        ByteArrayOutputStream baos = null;
        try {
            // 序列化
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
            byte[] bytes = baos.toByteArray();
            return bytes;
        } catch (Exception e) {
            logger.error(e);
            throw new SerializationException("序列化异常",e);
        }
    }

    /**
     * 反序列化
     * 
     * @param bytes
     * @return
     * @throws Exception
     */
    public static Object unSerialize(byte[] bytes) {
        if(bytes == null) return null;
        ByteArrayInputStream bais = null;
        try {
            // 反序列化
            bais = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bais);
            return ois.readObject();
        } catch (Exception e) {
            logger.error(e);
            throw new SerializationException("反序列化异常",e);
        }
    }
}

